/*
 * 文件名：WechatLogAspect.java
 * 版权：Copyright 2012-2016 广州宝锶信息技术有限公司
 * 创建人：曾毅
 * 创建时间：2017年8月24日 下午2:08:43
 * 修改人：
 * 修改时间：
 * 修改内容：
 */
package com.baothink.weichat.aspectj;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baothink.weichat.dto.base.FeedBackEventBaseDto;
import com.baothink.weichat.dto.common.WeiChatLoginUser;
import com.baothink.weichat.dto.common.WeichatVisualConstant;
import com.baothink.weichat.handler.WechatEventHandler;
import com.sun.corba.se.pept.transport.EventHandler;

import lombok.extern.slf4j.Slf4j;
import sun.util.logging.resources.logging;

/**
 * 微信访问日志记录
 * 
 * @author 曾毅
 * @version 1.0,2017年8月24日 下午2:08:43
 * @since visual-web-wechat 0.0.1
 */
@Aspect
@Component
@Slf4j
public class WechatLogAspect {
	
	@Autowired(required=false)
	private WechatEventHandler eventHandler;
	

	@Pointcut("execution (public * *(..))")
	public void controllerAspect() {
	}
	
//	@Before("execution(* com..controller.*(..))")
	public void beforeAdvice(JoinPoint jp){
		System.out.println("前置通知");
		System.out.println(jp.getArgs());
	}
	
//	@Before("controllerAspect()")
	public void doBefore(JoinPoint joinPoint) {
		System.out.println("==========执行controller前置通知===============");
		if (this.log.isInfoEnabled())
			this.log.info("before " + joinPoint);
	}
	
	@Around("execution(* com.baothink.weichat.controller.FeedBackController.feedBack(..))")
	public Object aroundAdvice(ProceedingJoinPoint jp){
		System.out.println("环绕通知 invoking...");
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		Class<?> target = jp.getTarget().getClass();
		MethodSignature signature = (MethodSignature) jp.getSignature();
		String method=signature.getMethod().getName();
		System.out.println(method);
		System.out.println(request.getRequestURI());
		
		
		WeiChatLoginUser loginUser = (WeiChatLoginUser) request.getSession().getAttribute(WeichatVisualConstant.WECHAT_USER_FLAG);
		if(null!=loginUser){
			System.out.println(loginUser.getUserId());
		}
		
		try {
			final Object event = jp.proceed();
			if(null!=eventHandler&&event!=null){
				ExecutorService executorService = Executors.newCachedThreadPool();
				executorService.submit(new Runnable() {
					@Override
					public void run() {
						eventHandler.eventHandler((FeedBackEventBaseDto) event);
					}
				});
			}
			return event;
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return null;
	}
}
